﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public class ICACDocument
    {


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(ElingOA6_Admin_DSJCJ.Model.ICACDocument model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_ICACDocument(");
            strSql.Append("FTitle,FAttachSvrFileName,FAttachOldFileName,FAttachDesc,FAttachFileExt,FInsDate)");
            strSql.Append(" values (");
            strSql.Append("@FTitle,@FAttachSvrFileName,@FAttachOldFileName,@FAttachDesc,@FAttachFileExt,@FInsDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,200),
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FInsDate", SqlDbType.DateTime)};
            parameters[0].Value = model.FTitle;
            parameters[1].Value = model.FAttachSvrFileName;
            parameters[2].Value = model.FAttachOldFileName;
            if (model.FAttachDesc == "")
            {
                parameters[3].Value = DBNull.Value;
            }
            else {
                parameters[3].Value = model.FAttachDesc;
            }
            parameters[4].Value = model.FAttachFileExt;
            parameters[5].Value = model.FInsDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetPath()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FTitle,FAttachSvrFileName,FAttachOldFileName,FAttachDesc,FAttachFileExt,FInsDate");
            strSql.Append(" from dbo.t_ICACDocument");
            

            return DbHelperSQL.Query(strSql.ToString());
            
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ICACDocument GetModel(string title)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FICACDocumentID,FTitle,FAttachSvrFileName,FAttachOldFileName,FAttachDesc,FAttachFileExt,FInsDate from dbo.t_ICACDocument ");
            strSql.Append(" where FTitle=@FTitle");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,200)
			};
            parameters[0].Value = title;

            ElingOA6_Admin_DSJCJ.Model.ICACDocument model = new ElingOA6_Admin_DSJCJ.Model.ICACDocument();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ICACDocument DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ICACDocument model = new ElingOA6_Admin_DSJCJ.Model.ICACDocument();
            if (row != null)
            {
                if (row["FICACDocumentID"] != null && row["FICACDocumentID"].ToString() != "")
                {
                    model.FICACDocumentID = decimal.Parse(row["FICACDocumentID"].ToString());
                }
                if (row["FTitle"] != null)
                {
                    model.FTitle = row["FTitle"].ToString();
                }
                if (row["FAttachSvrFileName"] != null)
                {
                    model.FAttachSvrFileName = row["FAttachSvrFileName"].ToString();
                }
                if (row["FAttachOldFileName"] != null)
                {
                    model.FAttachOldFileName = row["FAttachOldFileName"].ToString();
                }
                if (row["FAttachDesc"] != null)
                {
                    model.FAttachDesc = row["FAttachDesc"].ToString();
                }
                if (row["FAttachFileExt"] != null)
                {
                    model.FAttachFileExt = row["FAttachFileExt"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
            }
            return model;
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_ICACDocument");
            strSql.Append(" where FTitle=@FTitle");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,200)
			};
            parameters[0].Value = title;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.ICACDocument model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ICACDocument set ");
            strSql.Append("FTitle=@FTitle,");
            strSql.Append("FAttachSvrFileName=@FAttachSvrFileName,");
            strSql.Append("FAttachOldFileName=@FAttachOldFileName,");
            strSql.Append("FAttachDesc=@FAttachDesc,");
            strSql.Append("FAttachFileExt=@FAttachFileExt,");
            strSql.Append("FInsDate=@FInsDate");
            strSql.Append(" where FICACDocumentID=@FICACDocumentID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,200),
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FICACDocumentID", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FTitle;
            parameters[1].Value = model.FAttachSvrFileName;
            parameters[2].Value = model.FAttachOldFileName;
            if (model.FAttachDesc == "")
            {
                parameters[3].Value = DBNull.Value;
            }
            else
            {
                parameters[3].Value = model.FAttachDesc;
            }
            parameters[4].Value = model.FAttachFileExt;
            parameters[5].Value = model.FInsDate;
            parameters[6].Value = model.FICACDocumentID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <param name="strAdCds"></param>
        /// <param name="strEntityNo"></param>
        /// <param name="title"></param>
        /// <param name="icacDocumentID"></param>
        /// <returns></returns>
        public List<string> GetSrvName(string title, decimal icacDocumentID)
        {
            List<string> listStr = new List<string>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAttachSvrFileName");
            strSql.Append(" FROM dbo.t_ICACDocument  where FTitle in('");
            strSql.Append(title);
            strSql.Append("')");
            strSql.Append(" and FICACDocumentID=@FICACDocumentID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FICACDocumentID", SqlDbType.Decimal,13)};
            parameters[0].Value = icacDocumentID;
            using (SqlDataReader sdr = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters))
            {
                while (sdr.Read())
                {
                    listStr.Add(sdr[0].ToString());
                }
            }

            return listStr;
        }

    }
}
